#include <boost/asio.hpp>
#include <iostream>
#include <string>

using boost::asio::ip::tcp;
//typedef boost::asio::ip::tcp tcp;
int main()
{
    try{
        boost::asio::io_context ioc;
        
        // 解析服务器地址
        tcp::resolver resolver(ioc);
        tcp::resolver::results_type endpoints = resolver.resolve("www.baidu.com", "http");

        // 创建socket连接
        tcp::socket socket(ioc);
        boost::asio::connect(socket, endpoints);

        // 发送http请求
        std::string request = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n";
        boost::asio::write(socket, boost::asio::buffer(request));

        // 读取http响应
        boost::asio::streambuf response;
        // 
        boost::asio::read_until(socket, response, "\r\n\r\n");
        // 所有数据尽可能传输完毕 直到发生错误 或者 连接关闭EOF
        // 会导致等待很多数据，可能导致程序挂起
        //boost::asio::read(socket, response, boost::asio::transfer_all()); 

        std::cout << &response << std::endl;
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }

}